package com.exam.sms.mq;

import com.alibaba.fastjson.JSON;
import com.exam.sms.config.SmsProperties;
import com.exam.sms.utils.SmsUtils;
import com.rabbitmq.http.client.domain.ExchangeType;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @ClassName : SmsListener
 * @Description : 消息监听
 * @Author : WHB
 */
@Component
@EnableConfigurationProperties(SmsProperties.class)
public class SmsListener {

    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private SmsProperties prop;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "sms.code.queue" ,durable = "true"),
            exchange = @Exchange(name = "exam.sms.exchange", type = ExchangeTypes.TOPIC),
            key = "sms.code"
    ))
    public void ListenInsertOrUpdate(Map<String, String> msg){
        if (CollectionUtils.isEmpty(msg)){
            return;
        }
        String phone = msg.remove("phone");
        smsUtils.SendSms(phone,prop.getSignName(),prop.getTemplateCode(), JSON.toJSONString(msg));
    }
}
